<?php	
	/*
	 *	Sample Module template for "OH"
	 *  
	 *  use for write develop module interface
	 *  to encampusulate opensource script into "OH"
	 */
	
	// Interface function	
	function oh_module_frecord($tokens) {
		global $oh_object_frecord;
		return $oh_object_frecord;	
	} 

	// Option file required
	//require_once($oh['path']."...");
	//require_once($oh['path']."...");
	
	// Main class of module
	class oh_class_frecord {		
		function oh_class_frecord() {
		}
		function prepare_file($file) {
			if (!file_exists($file)) {
				file_put_contents($file,"");
			}		
		}
		function get($file) {
			$this->prepare_file($file);
			$content = file_get_contents($file);
			return explode("\n",$content);
		}		
		function put($file, $array) {
			$this->prepare_file($file);
			$data = implode("\n",$array);
			file_put_contents($file,$data);
		}
		function prepend($file, $record) {
			$this->prepare_file($file);
			$content = file_get_contents($file);
			$records = explode("\n",$content);
			$records = (array_merge(array($record),$records));
			$this->put($file,$records);
		}
		function append($file, $record) {
			$this->prepare_file($file);
			$content = file_get_contents($file);
			$records = explode("\n",$content);
			$records = (array_merge($records,array($record)));
			$this->put($file,$records);
		}
		function delete($file, $line) {
			$this->prepare_file($file);
			$content = file_get_contents($file);
			$records = explode("\n",$content);
			$records_new = array();
			foreach($records as $line0=>$record) {
				if ($line0 != $line) {
					$records_new[$line0] = $record;
				}
			}
			$this->put($file,$records_new);
		}		
		function move($file, $line0, $line1) {
			$this->prepare_file($file);
			$content = file_get_contents($file);
			$records = explode("\n",$content);
			$temp = $records[$line0];
			$records[$line0] = $records[$line1];
			$records[$line1] = $temp;
			$this->put($file,$records);
		}		
		function modify($file, $line, $newrecord) {
			$this->prepare_file($file);
			$content = file_get_contents($file);
			$records = explode("\n",$content);
			$records_new = array();
			foreach($records as $line0=>$record) {
				if ($line0 == $line) {
					$records_new[$line0] = $newrecord;
				} else {
					$records_new[$line0] = $record;
				}
			}
			$this->put($file,$records_new);
		}		


	}
	
	// Main object of module
	global $oh_object_frecord;
	$oh_object_frecord = new oh_class_frecord();
?>
